using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.CompilerServices;

namespace Microsoft.ContactsBridge.Interop
{
    [StructLayout(LayoutKind.Sequential)]
    public struct SIZE
    {
        public int cx;
        public int cy;

        public SIZE(int cx, int cy)
        {
            this.cx = cx;
            this.cy = cy;
        }
    } 

    [
        ComImport,
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
        Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1")
    ]
    interface IExtractImage
    {
        void GetLocation(
            [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex=1)] StringBuilder pszPathBuffer,
            int cch,
            ref int pdwPriority,
            ref SIZE prgSize,
            int dwRecClrDepth,
            ref int pdwFlags);

        void Extract(out IntPtr phBmpThumbnail);
    }

    [
        ComImport,
        TypeLibType(TypeLibTypeFlags.FCanCreate),
        ClassInterface(ClassInterfaceType.None),
        Guid("8082C5E6-4C27-48ec-A809-B8E1122E8F97")
    ]
    internal class ContactShellExtensionHandlerRcw : IExtractImage, IPersistStream
    {
        #region IPersistStream Members

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        public virtual extern void GetClassID(out Guid pClassID);

        [
            PreserveSig,
            MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)
        ]
        public virtual extern HRESULT IsDirty();

        [
            PreserveSig,
            MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)
        ]
        public virtual extern HRESULT Load(IStream pstm);

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        public virtual extern void Save(IStream pstm, bool fClearDirty);

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        public virtual extern void GetSizeMax(out ulong pcbSize);

        #endregion
    
        #region IExtractImage Members

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        public virtual extern void GetLocation(StringBuilder pszPathBuffer, int cch, ref int pdwPriority, ref SIZE prgSize, int dwRecClrDepth, ref int pdwFlags);

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        public virtual extern void Extract(out IntPtr phBmpThumbnail);

        #endregion
    }

}
